<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// check if the user is logged-in
include_once '../utils/includer.php';
includeFile('administrator/authentication.php');
isAuthorized();

includeFileOnce("utils/redirect.php");
includeFileOnce("protected/database/MySqlDAO.php");
includeFile("protected/messages/it/messages.php");
includeFileOnce("utils/directories.php");

$messages = getBundleMessages();
$mySqlDAO = new MySqlDAO();

$parent_dir = $_GET['parent_dir'];
if (isset($_GET['idc']))
    $idc = $_GET['idc'];
if (isset($_GET['idf']))
    $idf = $_GET['idf'];
if (isset($_GET['idl']))
    $idl = $_GET['idl'];


if (empty($_POST['isToDelete'])) {
    if (!empty($idc))
        $name = $mySqlDAO->getCategoryName($idc);
    else if (!empty($idf))
        $name = $mySqlDAO->getFileTitle($idf);
    else if (!empty($idl))
        $name = $mySqlDAO->getLinkTitle($idl);
}
else if (!empty($_POST['isToDelete'])) {

    // delete category
    if (!empty($idc)) {
        $dbPath = $mySqlDAO->getCategoryPath($idc);
        $name = $mySqlDAO->getCategoryName($idc);
        $res = $mySqlDAO->deleteCategory($idc);
        if (!empty($res)) {
            $path = getRootDir() . DIRECTORY_SEPARATOR . $name;
            
            include 'deleteElementController.php';
            deldir($path);
            //rmdir($path) or die("ERROR: Cannot delete directory " . $path);
            redirect("index.php?idc=$parent_dir", FALSE);
        } else
            die("ERROR: Cannot delete category " . $name);
    }
    // delete file
    else if (!empty($idf)) {
        $dbPath = $mySqlDAO->getFilePath($idf);
        $fileName = $mySqlDAO->getFileName($idf);
        $res = $mySqlDAO->deleteFile($idf);
        if (!empty($res)) {
            $path = getRootDir() . $dbPath . $fileName;
            unlink($path) or die("ERROR: Cannot delete file " . $path);
            redirect("index.php?idc=$parent_dir", FALSE);
        } else
            die("ERROR: Cannot delete file " . $fileName);
    }
    // delete link
    else if (!empty($idl)) {
        $res = $mySqlDAO->deleteLink($idl);
        if (!empty($res))
            redirect("index.php?idc=$parent_dir", FALSE);
        else
            die("ERROR: Cannot delete link");
    } else {
        die("ERROR: Inserire tutti i dati. <a href=javascript:history.back(-1)>Indietro</a>");
    }
}
?>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">
        <title>WebDoc - Delete element</title>
    </head>
    <body>
        <?php
        if (isset($idc))
            $formaction = "deleteelement.php?parent_dir=$parent_dir&idc=$idc";
        if (isset($idf))
            $formaction = "deleteelement.php?parent_dir=$parent_dir&idf=$idf";
        if (isset($idl))
            $formaction = "deleteelement.php?parent_dir=$parent_dir&idl=$idl";
        ?>
        <div id="page" class="rounded">
            <div id="header">
                <div id="logo">Webdoc</div>
            </div>
            <div id="commandbar">
                <div id="menubar">
                    <ul>
                        <li><a href="index.php?idc=<?php echo $parent_dir; ?>" title="Back">
                                <img src="../images/back_arrow.png"/></a></li>
                        <li><a href="index.php?idc=1" title="Home">
                                <img src="../images/home.png"/></a></li>
                    </ul>
                </div>
                <div id="topbar">
                    <a href="../protected/ActionDispatcher.php?action=logout">Esci</a>
                </div>
                <hr/>
            </div>
            <div class="contents">
                <form method="post" enctype="multipart/form-data" action="<?php echo $formaction ?>">
                    <table border="0" cellpadding="1" cellspacing="1" class="box">
                        <tr>
                            <td valign="top">
                                <div class="delete_element ">
                                <?php
                                if (!empty($idc)) {
                                    echo sprintf($messages['delete.category'],$name);
                                    echo "<div class=warning>".$messages['delete.category.warning']."</div>";
                                }
                                else if (!empty($idf))
                                    echo sprintf($messages['delete.file'],$name);
                                else if (!empty($idl))
                                    echo sprintf($messages['delete.link'],$name);
                                ?>
                                </div>
                                <input type="hidden" name="isToDelete" value="true">
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <br>
                                <input type="submit" value="Si" />
                                <input type="button" value="No" onclick="javascript:history.back()"/>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
    </body>
</html>
